package controller.admins;

import com.alibaba.fastjson.JSON;
import dao.admins.IAnnouncementMan;
import dao.admins.impl.AnnouncementManImpl;
import pojo.Announcement;
import util.pagingOpt.impl.AnnPagingImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;

@WebServlet(name = "DoAnServlet", urlPatterns = "/doAn")
public class DoAnServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        IAnnouncementMan announcementMan = new AnnouncementManImpl();
        String action = request.getParameter("action");
        if ("anQuery".equals(action)){
            anQuery(request);
            request.getRequestDispatcher("page/announcement/anQuery.jsp").forward(request, response);
        }else if ("anQueryById".equals(action)){
            anQueryById(request, announcementMan);
            request.getRequestDispatcher("page/announcement/anUpdate.jsp").forward(request, response);
        }else if ("anUpdate".equals(action)){
            anUpdate(request, announcementMan);
            anQuery(request);
            request.getRequestDispatcher("page/announcement/anQuery.jsp").forward(request, response);
        }else if ("anAdd".equals(action)){
            anAdd(request, announcementMan);
            anQuery(request);
            request.getRequestDispatcher("page/announcement/anQuery.jsp").forward(request, response);
        }else if ("anDelById".equals(action)){
            anDelById(request, announcementMan);
            anQuery(request);
            request.getRequestDispatcher("page/announcement/anQuery.jsp").forward(request, response);
        }else if ("paging".equals(action)){
            PrintWriter out = response.getWriter();
            int pageNo = Integer.parseInt(request.getParameter("pageNo"));
            String pageSupportJson = JSON.toJSONStringWithDateFormat(paging(pageNo).getPageEntity(),"yyyy-MM-dd");
            out.write(pageSupportJson);
        }
    }

    private void anQuery(HttpServletRequest request){
        List<Announcement> announcements = paging(1).getList();
        request.setAttribute("pageData", announcements);
        request.setAttribute("pageNumber", request.getParameter("pageNumber"));
    }

    private void anQueryById(HttpServletRequest request, IAnnouncementMan announcementMan){
        long id = Integer.parseInt(request.getParameter("id"));
        request.setAttribute("An", announcementMan.queryAnnById(id));
    }

    private void anUpdate(HttpServletRequest request, IAnnouncementMan announcementMan){
        Announcement announcement = new Announcement();
        String date = request.getParameter("aDate") + " 00:00:00";
        AnnouncementManImpl.setAnn(announcement, Long.valueOf(request.getParameter("id")), request.getParameter("aTitle"), request.getParameter("aText"), Timestamp.valueOf(date));
        announcementMan.updateAnn(announcement);
    }

    private void anAdd(HttpServletRequest request, IAnnouncementMan announcementMan){
        Announcement announcement = new Announcement();
        String date = request.getParameter("aDate") + " 00:00:00";
        AnnouncementManImpl.setAnn(announcement, 0, request.getParameter("aTitle"), request.getParameter("aText"), Timestamp.valueOf(date));
        announcementMan.addAnn(announcement);
    }

    private void anDelById(HttpServletRequest request, IAnnouncementMan announcementMan){
        long id = Integer.parseInt(request.getParameter("id"));
        announcementMan.deleteAnn(id);
    }
    private AnnPagingImpl paging(int pageNo){
        String sqlQuery = "SELECT COUNT(*) FROM announcement;";
        String sqlPaging = "select * from announcement limit ?, ?;";
        AnnPagingImpl paging = new AnnPagingImpl(3, sqlQuery, sqlPaging);
        paging.setCurrentPage(pageNo);
        return paging;
    }
}
